﻿using UnityEngine;
using System.Collections;

public partial class PoliceFSM
{

    //-----------------------------------CloseAtkState------------------------------------------

    IEnumerator CloseAtkState()
    {
        float fDmgApplyTime = 0.0f; //Damage Delay
        int iWeaponType = 0;
        

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.CLOSEATTACK);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
        gameObject.transform.GetChild(1).gameObject.active = false;
        iWeaponType = gameObject.transform.GetChild(1).GetComponent<Animator>().GetInteger("iWeaponType");
        

        gameObject.GetComponent<AStar>().AStarStart(Calculate.GetMyTileIdx(transform.position), Calculate.GetMyTileIdx(m_vecDest));

        m_BestList = gameObject.GetComponent<AStar>().GetBestList();

        do
        {
            yield return null;

            if (m_ObjCloseAtkTarget == null)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                break;
            }
            else
            {
                if (m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD)
                {
                    gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                    m_ObjCloseAtkTarget = null;
                    break;
                }
            }
            

            Calculate.RotToDest(gameObject, m_ObjCloseAtkTarget.transform.position);


            fDmgApplyTime += Time.deltaTime;

            if (fDmgApplyTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length ) //if Animroof End, Apply Dmg
            {
                //Close Atk Damage Calcu
                m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.iCurrentHP -= 5;
                m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.iCurrentMorale -= 3;

                if (m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState != BASEUNIT_STATE.DEAD)
                m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.DAMAGE;

                Calculate.RotToDest(m_ObjCloseAtkTarget, gameObject.transform.position);
                fDmgApplyTime = 0.0f;

            }

            CloseAtkSound();
            CommonState();
            MoveOrder();
            Chat(CHATBUBBLE_STATE.ATTACK);

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.CLOSEATTACK);

        gameObject.transform.GetChild(1).gameObject.active = true;
        gameObject.transform.GetChild(1).GetComponent<Animator>().SetInteger("iWeaponType", iWeaponType);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    //-----------------------------------AimState------------------------------------------
    IEnumerator AImState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.AIM);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
        yield return null;

        float fRandShtSpeed = Random.Range(0.0f, 10 - gameObject.GetComponent<UnitStatus>().m_unitData.iShootSpeed);
        float fCurrentTime = 0.0f;

        bool bFired = false;  //사격여부
        float fFireTerm = 0.0f;   //연사속도 

        int iFireNum = Random.Range(1, gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iWeaponRate);   //한번의 사격상태에서 사격하는 횟수 
        int iCurrentFireNum = 0;  //현재사격횟수 
        float fMissFlag;



        //gameObject.GetComponent<PolyNavAgent>().Stop();
        Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);


        do
        {
            yield return null;

            if (m_ObjTarget == null
                || m_ObjTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD)
            {
                m_ObjTarget = null;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                break;
            }

            Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);

            if (fCurrentTime >= fRandShtSpeed)          //Fire State
            {
                fCurrentTime = 0.0f;
                fRandShtSpeed = 0.5f;
                    
                //Reload Change
                if (ReloadEnalbe())
                    break;

                //Ammo Out
                if (gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo <= 0)
                {
                    gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                    break;
                }

                bFired = true;
                //m_pOwner.transform.FindChild("SightUI").GetComponent<SightUI>().m_bFired = true;

                ++iCurrentFireNum;

                bool bFlag;
                Vector3 BulletPos = m_ObjTarget.transform.position;

                //------------Accu Check---------------
                if (Calculate.Acuur_Calculate(gameObject.GetComponent<UnitStatus>().Get_AcuurStat()))
                    bFlag = true;
                else
                {
                    bFlag = false;
                    float fRandX = Random.Range(-0.2f, 0.2f);
                    float fRandY = Random.Range(-0.2f, 0.2f);


                    fMissFlag = Random.Range(0.0f, 1.0f);

                    BulletPos = new Vector3(BulletPos.x + fRandX, BulletPos.y + fRandY, BulletPos.z);

                    if (fMissFlag >= 0.6f)
                    {
                        Vector3 FinDest = BulletPos;
                        FinDest = Vector3.Normalize(FinDest - gameObject.transform.position);


                        BulletPos = BulletPos + (FinDest * 10.0f);
                        fMissFlag = 0.0f;
                    }


                }



                //Bullet Create
                gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.FIRE);
                gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
                gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo -= 1;
                ObjFactory.CreateBullet(gameObject.transform.position, BulletPos, bFlag, gameObject, "Enemy");

                //GunfireSound
                transform.GetChild(1).GetComponent<AudioSource>().Play();

                Chat(CHATBUBBLE_STATE.ATTACK);
            }
            else
                fCurrentTime += Time.deltaTime;

            if (bFired == true)
            {           
                fFireTerm += Time.deltaTime;
            }

            if (fFireTerm >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {

                fFireTerm = 0.0f;

                if (iCurrentFireNum >= iFireNum)	//FireNum Comple
                {
                    gameObject.GetComponent<UnitStatus>().m_unitData.fCurrent_ShootSpeed = 0.0f;
                    gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                }
                else
                    bFired = false;

                gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.AIM);
                gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
            }

            //Act
            CommonState();
            MoveOrder();
            CloseAtkEnable();  
            

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.AIM);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }

    IEnumerator TanutState()
    {
        yield return null;

        CommonState();
    }

    IEnumerator ReloadState()
    {
        //필요한 변수 선언
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.RELOAD);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
        //gameObject.GetComponent<PolyNavAgent>().Stop();

        do
        {
            yield return null;

            CommonState();
            CloseAtkEnable();
            MoveOrder();

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.iHavMagazine -= 1;

                ObjFactory.CreateMagazine(gameObject.transform.position);

                gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo = gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iFullAmmo;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                break;
            }
            

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.RELOAD);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

}
